import axios from 'axios'
import {USERS_URL} from '../../components/Plug/URL'
const User = {
	namespaced:true,
	state:{
		MsbdList:[],
		loading:false,
		pageObj:{
			currentPage:1,
			total:null
		},
		isupdata:false,
		idarr:[]
	},
	getters:{},
	mutations:{
		setotal(state,payload){
			state.pageObj.total=payload
		},
		setcurrent(state,payload){
			state.pageObj.currentPage=payload
		},
		setisupdata(state,payload){
			state.isupdata=payload
		},
		setidarr(state,payload){
			let arr=[]
			for(var i=0;i<payload.length;i++){
				arr.push(payload[i].id)
			}
			state.idarr=arr
		}
    },
    actions:{ 
		getMsbdList({state,commit}){
			state.loading=true
			axios({
				method: 'get',
				url: USERS_URL,
				params: { transform: 1, order: 'id,desc', page: `${state.pageObj.currentPage},10`},
			}).then(data => {
				state.loading=false
				state.MsbdList = data.data.records 
				commit('setotal',data.data.results)
			})
		},
		jumpPage({state,commit,dispatch},page){
			commit('setcurrent',page)
			dispatch('getMsbdList')
		},
		save({state,commit,dispatch},form){
			return new Promise((proslve,project)=>{
				let method='post'
				let url=USERS_URL
				if(state.isupdata){
					url=USERS_URL+'/'+form.id
					method='put'
				}
				axios({
					method,
					url,
					data:form,
				}).then(data => {
					dispatch('getMsbdList')
					proslve()
				})
			})
		},
		delete({state,commit,dispatch},form){
			return new  Promise((proslve,project)=>{
				commit('setidarr',form)
				axios({
					method:'delete',
					url:USERS_URL+'/'+state.idarr.join(','),
				}).then(data => {
					dispatch('getMsbdList')
					proslve()
				})
			})
		}
	}
}
export default User